package com.hotelManagerSystem.shiro;

import com.hotelManagerSystem.model.Permission;
import com.hotelManagerSystem.model.Role;
import com.hotelManagerSystem.model.Staff;
import com.hotelManagerSystem.model.User;
import com.hotelManagerSystem.service.IRolePermissionService;
import com.hotelManagerSystem.service.IStaffRoleService;
import com.hotelManagerSystem.service.IStaffService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;

/**
 * @author treasy
 * @description 用于管理员登录和授权
 */
public class ShiroRealmAdmin extends AuthorizingRealm {
    @Autowired
    IStaffService iStaffService;
    @Autowired
    IStaffRoleService iStaffRoleService;
    @Autowired
    IRolePermissionService iRolePermissionService;
    /***
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.创建shiro授权对象
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        //2.获取认证用户信息
        Staff staff = (Staff) principalCollection.getPrimaryPrincipal();
        //3.获取用户id
        int staffId=staff.getStaffId();
        //4.通过id查询用户所有角色信息
        List<Role> roleList = iStaffRoleService.queryAllRoleByStaffId(staffId);
        HashSet<String> roles = new HashSet<>();
        for (Role role:roleList){
            roles.add(role.getRoleCode());
        }
        //5.通过id查询用户所有权限信息
        List<Permission> permissionList = iRolePermissionService.queryAllPermissionByStaffId(staffId);
        HashSet<String> permissions = new HashSet<>();
        for (Permission permission:permissionList){
            permissions.add(permission.getPermissionCode());
        }
        //6.收集角色和权限信息
        authorizationInfo.addRoles(roles);
        authorizationInfo.addStringPermissions(permissions);


        return authorizationInfo;
    }

    /***
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1.通过token获取用户名
        String username = (String) authenticationToken.getPrincipal();
        //2.查询customer的user数据库获取用户信息
        Staff staff = iStaffService.queryByUsername(username);
        //3.shiro判断用户
        if (staff==null){
            return null;//shiro底层抛出UnknownAccountException异常
        }
        //4.判断密码
        ByteSource salt = ByteSource.Util.bytes(staff.getSalt());//通过shiro自带工具将盐值转换为byte类型
        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(staff,staff.getPassword(),salt,getName());
        return info;
    }
}
